import { create } from 'zustand'
import { createJSONStorage, persist } from 'zustand/middleware'

interface State {
  count: number
  token: string
}

interface Action {
  /**
   * 加法
   */
  increment: () => void
  /**
   * 登录
   */
  login: (data: Record<string, any>) => Promise<void>
}

export const useMainStore = create<State & Action>()(
  persist(
    (set, get) => ({
      count: 0,
      token: '',
      increment: () => set({ count: get().count + 1 }),
      login: async data => {
        set({ token: 'Bearer Token' })
      }
    }),
    {
      name: 'main',
      storage: createJSONStorage(() => localStorage),
      partialize: state => ({ token: state.token })
    }
  )
)
